<?php

/**
 * This is the model class for table "{{pro_discount}}".
 *
 * The followings are the available columns in table '{{pro_discount}}':
 * @property integer $zpd_id
 * @property integer $zp_id
 * @property integer $zpd_start_time
 * @property integer $zpd_end_time
 * @property double $zpd_discount
 * @property integer $zpd_status
 * @property integer $inputtime
 */
class ProDiscount extends CActiveRecord
{
	//行业一级分类 ，用于前台筛选
	public $category_top;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return ProDiscount the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{pro_discount}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zp_id, zm_id, zpd_start_time, zpd_end_time, zpd_status, zcr_id, inputtime', 'numerical', 'integerOnly'=>true),
			array('zpd_discount', 'numerical'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zpd_id, zp_id, zm_id, zpd_start_time, zpd_end_time, zpd_discount, zpd_status, zcr_id, inputtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'product'=>array(self::BELONGS_TO, 'Product', 'zp_id'),//关联产品
				'company'=>array(self::BELONGS_TO, 'Company', 'zm_id'),//关联产品
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zpd_id' => 'Zpd',
			'zp_id' => 'Zp',
			'zm_id' => 'Zm',
			'zpd_start_time' => 'Zpd Start Time',
			'zpd_end_time' => 'Zpd End Time',
			'zpd_discount' => 'Zpd Discount',
			'zpd_status' => 'Zpd Status',
			'zcr_id' => 'Zcr Id',
			'inputtime' => 'Inputtime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zpd_id',$this->zpd_id);
		$criteria->compare('zp_id',$this->zp_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zpd_start_time',$this->zpd_start_time);
		$criteria->compare('zpd_end_time',$this->zpd_end_time);
		$criteria->compare('zpd_discount',$this->zpd_discount);
		$criteria->compare('zpd_status',$this->zpd_status);
		$criteria->compare('zcr_id',$this->zcr_id);
		$criteria->compare('inputtime',$this->inputtime);

		//行业一级分类筛选，用于前台
		if(!empty($this->category_top)){
			$criteria->join .= ' LEFT JOIN {{product}} p ON p.zp_id=t.zp_id ';
			$criteria->join .= ' LEFT JOIN {{category}} zca ON zca.zca_id=p.zca_id ';
			$criteria->addCondition(" (zca.zca_id={$this->category_top} OR zca.zca_parentid={$this->category_top} OR zca.zca_topid={$this->category_top})");
		}else{
			$criteria->compare('zp_id',$this->zp_id);
		}
		
		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}